package com.example.vendingmachine;

import controller.StateC;
import controller.UIC;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity {

	// Data
	private int penge = 0;
	private int Betalt = 0;
	private int refund = 0;

	public UIC ui = new UIC();

	// Buttons
	Button bCoffee;
	Button bTea;
	Button bChocolate;
	Button bBrew;
	Button bRemove;
	Button b1kr;
	Button b2kr;
	Button b5kr;
	Button b10kr;
	Button b20kr;

	// TextView
	TextView tInfor;
	TextView tRefund;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);

		// Text
		tInfor = (TextView) findViewById(R.id.tInfor);
		tRefund = (TextView) findViewById(R.id.tRefund);
		tInfor.setText("Select Drink");
		tRefund.setText("");

		// Buttons
		bCoffee = (Button) findViewById(R.id.bCofee);
		bTea = (Button) findViewById(R.id.bTea);
		bChocolate = (Button) findViewById(R.id.bChocolate);
		bBrew = (Button) findViewById(R.id.bBrew);
		bRemove = (Button) findViewById(R.id.bReturn);
		b1kr = (Button) findViewById(R.id.b1kr);
		b2kr = (Button) findViewById(R.id.b2kr);
		b5kr = (Button) findViewById(R.id.b5kr);
		b10kr = (Button) findViewById(R.id.b10kr);
		b20kr = (Button) findViewById(R.id.b20kr);

		// buttonState
		enableMoneyButtons(false);
		bBrew.setEnabled(false);
		bRemove.setEnabled(false);

		// Choose Drink (group)
		bCoffee.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ui.handleEvent("Coffee");
				tInfor.setText("6 kr.");
				penge = StateC.getPrice();

				// Aktiver buttos
				enableMoneyButtons(true);

				// Deaktiver Butoons
				bCoffee.setEnabled(false);
				bTea.setEnabled(false);
				bChocolate.setEnabled(false);

			}
		});

		bTea.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ui.handleEvent("Tea");
				tInfor.setText("5 kr.");
				penge = StateC.getPrice();

				// Aktiver buttos
				enableMoneyButtons(true);

				// Deaktiver Butoons
				bCoffee.setEnabled(false);
				bTea.setEnabled(false);
				bChocolate.setEnabled(false);

			}
		});

		bChocolate.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				ui.handleEvent("Chocolate");
				tInfor.setText("8 kr.");
				penge = StateC.getPrice();

				// Aktiver buttos
				enableMoneyButtons(true);

				// Deaktiver Butoons
				bCoffee.setEnabled(false);
				bTea.setEnabled(false);
				bChocolate.setEnabled(false);

			}
		});

		bBrew.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ui.handleEvent("brew");
				tInfor.setText("Drink is ready! press 'Return'");

				// Aktiver
				bRemove.setEnabled(true);

				// DeAktiver buttons
				bBrew.setEnabled(false);
				enableMoneyButtons(false);

			}
		});

		bRemove.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ui.handleEvent("Remove");
				tInfor.setText("Select Drink");
				tRefund.setText("");
				Betalt = 0;

				// Aktiver
				bCoffee.setEnabled(true);
				bTea.setEnabled(true);
				bChocolate.setEnabled(true);

				// DeAktiver buttons
				bRemove.setEnabled(false);
				bBrew.setEnabled(false);
				enableMoneyButtons(false);

			}
		});

		// Coins

		b1kr.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Betalt += 1;
				System.out.println("Betalt = " + Betalt);
				tInfor.setText("You have paid: " + Betalt + "kr. ");

				if (Betalt >= penge) { // Making Decisions (if Statement)
					refund = Betalt - penge;
					tInfor.setText("Total Paid: " + Betalt + "kr. "
							+ ", Pleas press brew ");
					tRefund.setText("Refund: " + refund);
					ui.handleEvent(Betalt);

					// Aktiver
					bBrew.setEnabled(true);

					// DeAktivere
					enableMoneyButtons(false);

				}

			}
		});

		b2kr.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Betalt += 2;

				System.out.println("Betalt = " + Betalt);
				tInfor.setText("You have paid, " + Betalt + "kr. ");

				if (Betalt >= penge) {
					refund = Betalt - penge;

					tRefund.setText("Refund: " + refund);
					tInfor.setText("Total Paid: " + Betalt + "kr. "
							+ ", Pleas press brew ");
					ui.handleEvent(Betalt);

					// Aktiver
					bBrew.setEnabled(true);

					// DeAktivere
					enableMoneyButtons(false);
				}

			}
		});

		b5kr.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Betalt += 5;

				System.out.println("Betalt = " + Betalt);
				tInfor.setText("You have paid, " + Betalt + "kr. ");

				if (Betalt >= penge) {
					refund = Betalt - penge;

					tRefund.setText("Refund: " + refund);
					tInfor.setText("Total Paid: " + Betalt + "kr. "
							+ ", Pleas press brew ");
					ui.handleEvent(Betalt);

					// Aktiver
					bBrew.setEnabled(true);

					// DeAktivere
					enableMoneyButtons(false);
				}

			}
		});

		b10kr.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Betalt += 10;

				System.out.println("Betalt = " + Betalt);
				tInfor.setText("You have paid, " + Betalt + "kr. ");

				if (Betalt >= penge) {
					refund = Betalt - penge;

					tRefund.setText("Refund: " + refund);
					tInfor.setText("Total Paid: " + Betalt + "kr. "
							+ ", Pleas press brew ");
					ui.handleEvent(Betalt);

					// Aktiver
					bBrew.setEnabled(true);

					// DeAktivere
					enableMoneyButtons(false);
				}

			}
		});

		b20kr.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Betalt += 20;

				System.out.println("Betalt = " + Betalt);
				tInfor.setText("You have paid, " + Betalt + "kr. ");

				if (Betalt >= penge) {
					refund = Betalt - penge;

					tRefund.setText("Refund: " + refund);
					tInfor.setText("Total Paid: " + Betalt + "kr. "
							+ ", Pleas press brew ");
					ui.handleEvent(Betalt);

					// Aktiver
					bBrew.setEnabled(true);

					// DeAktivere
					enableMoneyButtons(false);
				}

			}
		});

	}

	private void enableMoneyButtons(boolean butttonSTate) {
		b1kr.setEnabled(butttonSTate);
		b2kr.setEnabled(butttonSTate);
		b5kr.setEnabled(butttonSTate);
		b10kr.setEnabled(butttonSTate);
		b20kr.setEnabled(butttonSTate);
	}
}
